package com.spring.cootask.web.service.impl;

import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spring.cootask.web.dao.FeTaskMapper;
import com.spring.cootask.web.dao.FeTaskModuleMapper;
import com.spring.cootask.web.dao.FeTaskUserMapper;
import com.spring.cootask.web.dao.TaskTablebaseDetailMapper;
import com.spring.cootask.web.model.CooTaskHead;
import com.spring.cootask.web.model.CooTaskUser;
import com.spring.cootask.web.model.FeTask;
import com.spring.cootask.web.model.FeTaskModule;
import com.spring.cootask.web.model.FeTaskUserWithBLOBs;
import com.spring.cootask.web.model.TaskTablebaseDetail;
import com.spring.cootask.web.service.FeTaskService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WoJunyu on 2016/9/20.
 */
@Service
public class FeTaskServiceImpl implements FeTaskService{

    @Resource
    private FeTaskMapper feTaskMapper;

    @Resource
    private FeTaskUserMapper feTaskUserMapper;    
		
    @Resource
    private TaskTablebaseDetailMapper taskTablebaseDetailMapper;

    @Resource
    private FeTaskModuleMapper feTaskModuleMapper;
    
    @Override
    public List<FeTask> selectTaskByParm(FeTask data) {
        return feTaskMapper.selectTaskByParm(data);
    }

    @Override
    public List<FeTask> selectTaskTransByParm(FeTask data) {
         List<FeTask> list=feTaskMapper.selectTaskTransByParm(data);
         for(FeTask feTask:list){
        	 if(feTask.getContentModule()==null){
        		 feTask.setContentModule(new String[0]);
        	 }
        	 if(feTask.getTitleModule()==null){
        		 feTask.setTitleModule(new String[0]);
        	 }
         }
         return list;
    }

    @Override
    public BaseResult saveTask(FeTask data, ShopOaSessionUtil.OaUser oaUser) {
        BaseResult ret = new BaseResult();
        if(oaUser==null||!ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
            ret.setStatus("0");
            ret.setMsg("请先登录");
            return ret;
        }
        if(!ApplicationUtils.isNotNull(data.getParentId())){
            data.setParentId(null);
        }
        int i;
        String id = ApplicationUtils.randomUUID();
        if(ApplicationUtils.isNotNull(data.getId())){//修改
        	id=data.getId();
            data.setChangedtime(new Date());
            data.setOperatorid(oaUser.getOa_user_id());
            data.setOperatorname(oaUser.getOa_user_name());
            i = feTaskMapper.updateByPrimaryKeySelective(data);

        }else{//新增            
            data.setId(id);
            data.initInfo();
            data.setTaskStatus(0);
            data.setInitialoperatorid(oaUser.getOa_user_id());
            data.setInitialoperatorname(oaUser.getOa_user_name());
            if(data.getTableType()==null){
            	data.setTableType(0);
            }
            i = feTaskMapper.insert(data);

        }
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("保存失败");
        }
        
        feTaskModuleMapper.deleteByTaskID(id);
        
        String[] titleModule=data.getTitleModule();      
        for(int j=0;j<titleModule.length;j++){
        	if(titleModule[j]!=null&&!titleModule[j].equals("")){
        		FeTaskModule feTaskModule=new FeTaskModule();
            	feTaskModule.setFeModuleid(titleModule[j]);
            	feTaskModule.setFeTaskid(id);
            	feTaskModule.setSort(j);
            	feTaskModuleMapper.insert(feTaskModule);
        	}
        }
        
        String[] contentModule=data.getContentModule();
        for(int j=0;j<contentModule.length;j++){
          if(contentModule[j]!=null&&!contentModule[j].equals("")){
        	FeTaskModule feTaskModule=new FeTaskModule();
        	feTaskModule.setFeModuleid(contentModule[j]);
        	feTaskModule.setFeTaskid(id);
        	feTaskModule.setSort(j+titleModule.length);
        	feTaskModuleMapper.insert(feTaskModule);
          }
        }
        
        return ret;
    }

    @Override
    public BaseResult deleteTask(String id) {
        BaseResult ret = new BaseResult();
        FeTask up = new FeTask();
        up.setId(id);
        up.setEntityavailablestate(20);
        int i = feTaskMapper.updateByPrimaryKeySelective(up);
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("删除失败");
        }
        return ret;
    }

    @Override
    public List<TaskTablebaseDetail> selectTableDetail(String taskId) {
        return taskTablebaseDetailMapper.selectByTablebaseID(taskId);
    }
    
    
    @Override
    public BaseResult commitPop(String taskUserID) throws Exception {
    	
    	FeTaskUserWithBLOBs feTaskUser=feTaskUserMapper.selectByPrimaryKey(taskUserID);
    	feTaskUser.setStatus(99);
    	
    	String feTaskID=feTaskUser.getFeTaskid();
    	int count=feTaskUserMapper.checkTaskIsOver(feTaskID);
    	if(count==0){
    		FeTask feTask=feTaskMapper.selectByPrimaryKey(feTaskID);
    		feTask.setTaskStatus(99);
    		feTaskMapper.updateByPrimaryKeySelective(feTask);
    	}
    	
        int effect = feTaskUserMapper.updateByPrimaryKeySelective(feTaskUser);
        return MyTools.checkResult(effect);
    }

}
